<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Libs\Helper\Status;
use App\Libs\JumpHtml;
use App\Libs\Status as DataStatus;
use Flaravel\Upload\Fupload;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CommonController extends Controller
{
    /**
     * 修改模型状态
     * @param string model 当前要修改的模型名称路径
     * @param unmber idvalue 当前修改的ID值
     * @param unmber status_name 修改状态的字段
     * @param unmber status_value 修改状态的值
     * @return json
     */
    public function changeStatus(Request $request)
    {

        if(!$request->has('model') || empty($request->model))
        {
            return $this->error('模型名称不能为空');
        }
        $model = 'App\\Models\\'.$request->model;
        if(!class_exists($model))
        {
            return $this->error('模型不存在');
        }
        $res = (new DataStatus(new $model,$request->idvalue,$request->status_name,$request->status_value))->change();
        if($res)
        {
            return $this->success(['message'=>'修改成功']);
        }
        return $this->error('修改失败');
    }

    /**
     * 修改模型数据
     * 默认采用sort排序 不可修改
     * @param string model 当前要修改的模型名称路径
     * @param unmber id 修改的ID
     * @param unmber sort 修改的排序值
     * @return json
     */
    public function editValue(Request $request)
    {
        if(!$request->has('model') || empty($request->model))
        {
            return $this->error('模型名称不能为空');
        }

        $model = 'App\\Models\\'.$request->model;
        if(!class_exists($model))
        {
            return $this->error('模型不存在');
        }
        $classModel = new $model;
        $res = $classModel->where('id',$request->id)->update([$request->filed =>$request->value]);
        if($res)
        {
            return $this->success(['status'=>1,'message'=>'修改成功']);
        }
        return $this->error('修改失败');
    }

    /**
     * 上传文件
     * @param files 文件字段名
     * @return url 文件链接
     */
    public function uploads(Request $request)
    {
        if($request->hasFile('file'))
        {
            $file = $request->file;
            $url = Fupload::upload($file,empty($request->path) ? : $request->path,$file->getClientOriginalExtension());
            if($url){
                if($request->type == 'layui')
                {
                     return ['code'=>0,'msg'=>'上传成功','data'=>['src'=>$url,'title'=>Str::after($url,'com/')]];
                }
                return $this->success(['status'=>1,'url'=>$url]);
            }
        }
        if($request->type == 'layui')
        {
             return ['code'=>-1,'msg'=>'上传失败'];
        }else{
            return $this->success(['status'=>-1]);
        }
    }

    /**
     * 删除 lauyui 文件
     * @param imgpath 图片文件路径
     * @param filepath 视频路径
     * @return json
     */
    public function deleteFile(Request $request){
        if($request->imgpath)
        {
            $res = Fupload::delete($request->imgpath);
            if($res){
               return ['code'=>0,'msg'=>'删除成功'];
            }
        }
        return ['code'=>-1,'msg'=>'上传失败'];
    }

    /**
     * 获取 运营跳转 的各种 html
     * @return string  html
     */
    public function jumpTypeData()
    {
        if(request()->jump_type)
        {
            return (new JumpHtml(request()->jump_type))->getJumpHtml();
        }
        return ['code'=>-1,'msg'=>'jump_type不能为空'];
    }
}
